//
//  HWPrepaidPhoneRecordsjiluViewController.m
//  HWPromoteProjectAPP
//
//  Created by 陈道斌 on 2020/3/9.
//  Copyright © 2020 陈道斌. All rights reserved.
//

#import "HWPrepaidPhoneRecordsjiluViewController.h"

//#import "HWjieSuanTableViewCell.h" //
#import "HWChongzhijiluModel.h"
#import "HWChongzhijiluCell.h"

@interface HWPrepaidPhoneRecordsjiluViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) UIView *hederView;



@property(strong,nonatomic)UIView *beijingView;
@property(strong,nonatomic)UILabel *zongjieLabel;
@property(strong,nonatomic)UILabel *qinshuLabel;
@property(strong,nonatomic)UILabel  *gongbiLabel;




// 导航的三个控件
@property(strong,nonatomic) UIView *daohaView;
@property(strong,nonatomic) UIButton *fanhuiBut;
@property(strong,nonatomic) UILabel *biatiuLabe;


//存储的数据
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;
@property(strong,nonatomic) NSMutableArray *dataMuArr;




@end

@implementation HWPrepaidPhoneRecordsjiluViewController




-(UITableView *)mainaTableVie {
    
    if  (_mainaTableVie == nil) {
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, -StatusBarHeight, kScreenW, kScreenH+StatusBarHeight-TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        // _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-44) style:UITableViewStylePlain];
        //   _mainaTableVie.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = beijingyanSe;
        
        // _mainaTableVie.backgroundColor = [UIColor whiteColor];
    }
    
    
    return   _mainaTableVie;
    
}
//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    self.navigationController.navigationBarHidden = NO;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    //   [self setnavigation];
    [self setnavigation];
    
    
    
    
    // 发送网络请求
    // [self sendNetworkRequest];
    //
    [self addrefreshControl];
    
    
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    
    
    
}




- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
    [self.mainaTableVie.mj_header beginRefreshing];
    
    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
    
    
    
}




// 下拉刷新
- (void)underRefresh {
    self.p = @"1";
    
    self.dataMuArr = [[NSMutableArray alloc]init];
    
    
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    NSString *uid =  [HWLocallyStoredData getLoginUserID];
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
    NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
    NSDictionary *parametes = @{              @"ios" : @"1",
                                              @"p" :self.p,
                                              @"ct" : @"app",
                                              @"ac" : @"recharge_list",
                                              @"t" : tStr,
                                              @"uid":uid,
                                              @"sign" :signStrMd5
    };
    
    
    
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        if ([codeStr intValue] == 1) {
            
            
            self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
            
            self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
            self.dataMuArr = [HWChongzhijiluModel  mj_objectArrayWithKeyValuesArray:response[@"data"][@"list"]];
            
            self.qinshuLabel.text = [NSString stringWithFormat:@"%@",response[@"data"][@"money"]];
            
            self.gongbiLabel.text = [NSString stringWithFormat:@"共计%@笔",response[@"data"][@"num"]];
            
            
            
            [self.mainaTableVie reloadData];
            
            
            NSLog(@"%@",response);
            
            
            
        } else {
            
            [MBProgressHUD showError:response[@"message"]];
            
            
            
        }
        
        
        
    } failure:^(NSString *error) {
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        
        
    }];
    
    
    
    
    
    
    
    
}





// 上拉加载

- (void)onRefresh {
    
    int p = [self.p intValue];
    
    int max_p = [self.max_p intValue];
    
    if (p > max_p) {
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
        
        return ;
        
    }
    
    NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
    NSString *uid =  [HWLocallyStoredData getLoginUserID];
    NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
    NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
    NSDictionary *parametes = @{              @"ios" : @"1",
                                              @"p" :self.p,
                                              @"ct" : @"app",
                                              @"ac" : @"recharge_list",
                                              @"t" : tStr,
                                              @"uid":uid,
                                              @"sign" :signStrMd5
    };
    
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        if ([codeStr intValue] == 1) {
            
            
            self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
            self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
            
            
            NSMutableArray *mutableArr = [HWChongzhijiluModel  mj_objectArrayWithKeyValuesArray:response[@"data"][@"list"]];
            for (int i = 0; i < mutableArr.count; i++) {
                
                
                [self.dataMuArr addObject:mutableArr[i]];
                
            }
            
            
            [self.mainaTableVie  reloadData];
            
            NSLog(@"%@",response);
            
            
            
            
        } else {
            
            [MBProgressHUD showError:response[@"message"]];
            
            
            
        }
        
        
        
    } failure:^(NSString *error) {
        // [SVProgressHUD dismiss];
        [self.mainaTableVie.mj_header endRefreshing];
        [self.mainaTableVie.mj_footer endRefreshing];
        
        
        //                       [MBProgressHUD showSuccess:@"登录失败"];
        //                       [MBProgressHUD hideHUD];
        
        
    }];
    
    
    
    
    
    
    
    
}
















// 设置导航
- (void)setnavigation {
    
    self.view.backgroundColor = beijingyanSe;
    
    UIView *backeview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 44+StatusBarHeight)];
    backeview.backgroundColor = [UIColor clearColor];
    self.daohaView = backeview;
    
    //   backeview.alpha = 0.1;
    [self.view addSubview:backeview];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10,StatusBarHeight+7, 30, 30);
    [button setImage:[UIImage imageNamed:@"fanhuibaiseanniu_ico"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
    self.fanhuiBut = button;
    [self.view  addSubview:button];
    
    UILabel *textlabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenW-70)/2, StatusBarHeight+12, 70, 17)];
    textlabel.text = @"充值记录";
    textlabel.textAlignment =  NSTextAlignmentCenter;
    textlabel.textColor = [UIColor whiteColor];
    textlabel.textColor = [UIColor whiteColor];
    textlabel.font = [UIFont systemFontOfSize:16];
    self.biatiuLabe  = textlabel;
    [self.view  addSubview:textlabel];
    
    
}



- (void)buttonTap {
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
    
}


// 设置UI
- (void)setupUI {
    
    
    //添加headerView
    [self addTabelViewHeaderView];
    
    
    
    
    
}

//添加headeView

- (void)addTabelViewHeaderView {
    
    
    
    [self.view addSubview:self.mainaTableVie];
    
    self.hederView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 44+StatusBarHeight+128)];
    self.hederView.backgroundColor = RGB(85, 89, 234);
    self.mainaTableVie.tableHeaderView = self.hederView;
    
    
    
    self.beijingView = [[UIView alloc]initWithFrame:CGRectMake(0,  44+StatusBarHeight, kScreenW, 128)];
    self.beijingView.backgroundColor = RGB(85, 89, 234);
    [self.hederView addSubview: self.beijingView ];
    
    
    self.zongjieLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 22, 170, 17)];
    self.zongjieLabel .text = @"总结算金额(元)";
    self.zongjieLabel .textAlignment =  NSTextAlignmentLeft;
    self.zongjieLabel .textColor = [UIColor whiteColor];
    self.zongjieLabel .font = [UIFont systemFontOfSize:12];
    [self.beijingView   addSubview: self.zongjieLabel];
    
    
    self.qinshuLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, self.zongjieLabel.bottom+1, 170, 45)];
    self.qinshuLabel .text = @"600.02";
    self.qinshuLabel .textAlignment =  NSTextAlignmentLeft;
    self.qinshuLabel .textColor = [UIColor whiteColor];
    self.qinshuLabel .font = [UIFont systemFontOfSize:32];
    [self.beijingView   addSubview: self.qinshuLabel];
    
    
    //  self.qinshuLabel
    self.gongbiLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, self.qinshuLabel.bottom+1, 280, 17)];
    self.gongbiLabel .text = @"共计 3 笔";
    self.gongbiLabel .textAlignment =  NSTextAlignmentLeft;
    self.gongbiLabel .textColor = [UIColor whiteColor];
    self.gongbiLabel .font = [UIFont systemFontOfSize:12];
    [self.beijingView   addSubview: self.gongbiLabel];
    
    
    
    
    
    
    
    
    
    
}



// 发送网络请求
//- (void)sendNetworkRequest {
//
//
//
//
//         NSString *tStr = [NSString stringWithFormat:@"%ld",[WMUtil getTimeSp]];
//         NSString *uid =  [HWLocallyStoredData getLoginUserID];
//         NSString *signStr = [NSString stringWithFormat:@"%@%@%@",uid,tStr,@"adesxloksy13s98adv"];
//         NSString  *signStrMd5  =   [WMUtil md5HexDigest:signStr];
//
//
//
//
//          NSDictionary *parametes = @{      @"ios" : @"1",
//                                       @"ct" : @"app",
//                                        @"ac" : @"jiesuan_list",
//                                        @"t" : tStr,
//                                        @"uid":uid,
//                                        @"sign" :signStrMd5
//                                        };
//
//
//            [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
//
//
//
//                  NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
//                  if ([codeStr intValue] == 1) {
//
//
//
//                       NSLog(@"%@",response);
//
//                      self.qinshuLabel.text = [NSString stringWithFormat:@"%@",response[@"data"][@"money"]];
//                      self.gongbiLabel.text =  [NSString stringWithFormat:@"共计 %@ 笔",response[@"data"][@"number"]];
//
//                      self.dataMuArr =  [HWBillingDetailsModel mj_objectArrayWithKeyValuesArray:response[@"data"][@"data"]];
//                      [self.mainaTableVie reloadData];
//
//                        NSLog(@"%@",response);
//
//
//
//
//                  } else {
//
//                    [MBProgressHUD showError:response[@"message"]];
//
//
//
//                  }
//
//
//
//              } failure:^(NSString *error) {
//                 // [SVProgressHUD dismiss];
//
//                   [MBProgressHUD showSuccess:@"登录失败"];
//                    [MBProgressHUD hideHUD];
//
//
//              }];
//
//
//
//
//
//
//
//
//}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    // return  self.dataMuArr.count;
    return  self.dataMuArr.count;
    
    
    
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    static NSString *cellID = @"cellID";
    
    HWChongzhijiluCell *cell  = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:@"HWChongzhijiluCell" owner:nil options:nil].firstObject;
        
    }
    
    HWChongzhijiluModel *model =  self.dataMuArr[indexPath.row];
    cell.model = model;
    
    
    return  cell;
    
    
    
    
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return  64;
    
    
    
}




- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    //设置separatorInset(iOS7之后)
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
}





@end
